<template>
  <div>
    <router-link tag="div" to="/" class="header-abs" v-show="isshowabs">
      <div class="header-abs-back iconfont">&#xe624;</div>
    </router-link>
    <div :style="opacityStyle" v-show="!isshowabs" class="header-fixed">
      景点详情
    </div>
  </div>
</template>

<script>
export default {
  name: "DetailHeader",
  data() {
    return {
      isshowabs: true,
      opacityStyle: {
        opacity: 0,
      },
    };
  },
  methods: {
    handleScroll() {
      const top = document.documentElement.scrollTop;
      if (top > 60) {
        let opacity = top/140;
        opacity = opacity > 1 ? 1:opacity;
        this.opacityStyle = {
            opacity
        }
        this.isshowabs = false;
      } else this.isshowabs = true;
      // documentElement 对应的是 html 标签而 body 对应的是 body 标签。
    },
  },
  activated() {
    window.addEventListener("scroll", this.handleScroll);
    // 由于是window对象，导致全部组件全部受到影响
  },
  deactivated(){
      window.removeEventListener("scroll", this.handleScroll)
    //   对全局事件进行解绑
  }
};
</script>

<style lang='stylus' scoped>
@font-face {
  font-family: 'iconfont';
  src: url('~@/assets/style/iconfont/iconfont.woff2?t=1636704678087') format('woff2'), url('~@/assets/style/iconfont/iconfont.woff?t=1636704678087') format('woff'), url('~@/assets/style/iconfont/iconfont.ttf?t=1636704678087') format('truetype');
}

.iconfont {
  font-family: 'iconfont' !important;
  font-size: 16px;
  font-style: normal;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

.header-abs {
  position: absolute;
  left: 0.2rem;
  top: 0.1rem;
  width: 0.8rem;
  height: 0.8rem;
  line-height: 0.8rem;
  text-align: center;
  border-radius: 0.4rem;
  background: #00bcd4;
  z-index: 99;

  .header-abs-back {
    color: #fff;
    font-size: 0.4rem;
  }
}

.header-fixed {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  overflow: hidden;
  height: 1rem;
  line-height: 0.86rem;
  text-align: center;
  line-height: 1rem;
  color: #fff;
  background: #00bcd4;
  font-size: 0.4rem;
}
</style>